package edu.zut.bailan.base.service.impl;

import edu.zut.bailan.base.dao.GenericDao;
import edu.zut.bailan.base.domain.BaseEntity;
import edu.zut.bailan.base.service.GenericManager;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
@Transactional
public class GenericManagerImpl<T extends BaseEntity<PK>, PK extends Serializable> implements GenericManager<T, PK> {
    protected GenericDao<T, PK> dao;

    public void delete(PK id) {
        this.dao.deleteById(id);
    }

    public Iterable<T> findAll() {
        return this.dao.findAll();
    }

    public Page<T> findAll(Pageable page) {
        Page<T> result = this.dao.findAll(page);
        return result;
    }

    public T findById(PK id) {
        return this.dao.findById(id).get();
    }

    public Iterable<T> save(Iterable<T> entities) {
        return this.dao.saveAll(entities);
    }

    public T save(T entity) {
        T data = this.dao.save(entity);
        return data;
    }

    @Override
    public void deleteAll() {
        this.dao.deleteAll();
    }

    @Override
    public long count() {
        return this.dao.count();
    }

}

